#include "C_JD2Unix.h"
#include "VBF_Common/VBF_Math/VBF_TimeConverter.h"


C_JD2Unix::C_JD2Unix(void)
{
}


C_JD2Unix::~C_JD2Unix(void)
{
}


long long C_JD2Unix::JD2Unix( double dJD )
{
	int year, month, day, hour, minute;
	double second;
	CVBF_TimeConverter::convertJDtoDT(dJD, year, month, day, hour, minute, second);
	double unixSecond = (day - 1) * 86400 + hour * 3600 + minute * 60 + second;
	for (int i = 1970; ; i++)
	{
		if (year > i)
		{
			if (((i % 4 == 0) && (i % 100 != 0)) || (i % 400 == 0))
				unixSecond += 366 * 86400;
			else
				unixSecond += 365 * 86400;
		}
		else
		{
			if (month == 1)
				;
			else if (month == 2)
				unixSecond += 31 * 86400;
			else
			{
				if (month == 3)
					unixSecond += 59 * 86400;
				else if (month == 4)
					unixSecond += 90 * 86400;
				else if (month == 5)
					unixSecond += 120 * 86400;
				else if (month == 6)
					unixSecond += 151 * 86400;
				else if (month == 7)
					unixSecond += 181 * 86400;
				else if (month == 8)
					unixSecond += 212 * 86400;
				else if (month == 9)
					unixSecond += 243 * 86400;
				else if (month == 10)
					unixSecond += 273 * 86400;
				else if (month == 11)
					unixSecond += 304 * 86400;
				else
					unixSecond += 334 * 86400;

				if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
					unixSecond += 86400;
			}
			break;
		}
	}
	return unixSecond;
}

double C_JD2Unix::Unix2JD( long long lUnix )
{
	double dJD = CVBF_TimeConverter::convertDTtoJD(1970, 1, 1, 0, 0, 0);
	dJD += lUnix/86400.0;
	return dJD;
}
